package com.example.helloworld.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;

import com.example.helloworld.R;

public class TitleButton extends Button {

	private int defaultColor =			0xFFFFFFFF;
	private float defaultSize =			20F;
	private float defaultScaleX =		1.0F;
	private Typeface defaultTypeFace =	Typeface.DEFAULT_BOLD;  
	private String titleText =			"";
	
	private Context context;
	private Paint paint;
	private boolean paintChanged =	false;
	
	public TitleButton(Context context) {
		super(context);
		
		this.context =	context;
		init();
	}
	
	public TitleButton(Context context, AttributeSet atts) {
		super(context, atts);
		
		this.context =	context;
		init();
	}
	
	public void init() {
		setBackgroundResource(R.drawable.title_button);
	
		paint =	new Paint();
		paint.setColor(defaultColor);
		paint.setAntiAlias(true);
		paint.setTextScaleX(defaultScaleX);
		paint.setTextSize(defaultSize);
		paint.setTypeface(defaultTypeFace);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		
		int action =	event.getAction();
		
		switch (action) {
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_DOWN:
			Toast.makeText(context, titleText, 1000).show();
			break;
		}
		
		invalidate();
		
		return true;
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int width =		getWidth();
		int height =	getHeight();
		
		if (paintChanged) {
			paint.setColor(defaultColor);
			paint.setTextScaleX(defaultScaleX);
			paint.setTextSize(defaultSize);
			paint.setTypeface(defaultTypeFace);
		}
		
		Rect bounds =	new Rect();
		paint.getTextBounds(titleText, 0, titleText.length(), bounds);
		
		float textWidth =	((float)width - bounds.width()) / 2.0F; 
		float textHeight =	(((float)(height - 4) + bounds.height()) / 2.0F) - 1.0F;
		
		canvas.drawText(titleText, textWidth, textHeight, paint);
	}

	public int getDefaultColor() {
		return defaultColor;
	}

	public void setDefaultColor(int defaultColor) {
		this.defaultColor = defaultColor;
		paintChanged =	true;
	}

	public float getDefaultSize() {
		return defaultSize;
	}

	public void setDefaultSize(float defaultSize) {
		this.defaultSize = defaultSize;
		paintChanged =	true;
	}

	public float getDefaultScaleX() {
		return defaultScaleX;
	}

	public void setDefaultScaleX(float defaultScaleX) {
		this.defaultScaleX = defaultScaleX;
		paintChanged =	true;
	}

	public Typeface getDefaultTypeFace() {
		return defaultTypeFace;
	}

	public void setDefaultTypeFace(Typeface defaultTypeFace) {
		this.defaultTypeFace = defaultTypeFace;
		paintChanged =	true;
	}

	public String getTitleText() {
		return titleText;
	}

	public void setTitleText(String titleText) {
		this.titleText = titleText;
		paintChanged =	true;
	}
}
